package cn.rht.study.util.redis;

import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 *JedisSetCache
 *@author liyuchen
 *@version 2020年4月15日 
 *
 */
@Component
public class JedisSetCache extends JedisCache {
    
	private String key = null;
	
	public JedisSetCache() {
		super();
	}
	JedisSetCache( JedisPool pool, String key ){
		super( pool );
		this.key = key;
	}

	public long setValue(String key,String... members){
		 Jedis jedis = null;
		 Long sadd = null;
		 try {
			jedis = pool.getResource();
			sadd = jedis.sadd(key, members);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(jedis!=null){
				jedis.close();
			}
		}
		return sadd;
	}
	
	public long delVal(String key,String...members){
		Jedis jedis = null;
		Long srem = null;
		 try {
			jedis = pool.getResource();
			srem = jedis.srem(key, members);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(jedis!=null){
				jedis.close();
			}
		}
		return srem;
	}
	
	public long delKey(String key){
		Jedis jedis = null;
		 try {
			jedis = pool.getResource();
			 //Long sadd = jedis.del(key);
			ScanParams scanParams = new ScanParams();
			scanParams.count(500);
			String cursor = "";
			while (!cursor.equals("0")) {
				ScanResult<String> scanResult = jedis.sscan(key, cursor, scanParams);
				cursor = scanResult.getStringCursor();
				List<String> result = scanResult.getResult();
				//long t1 = System.currentTimeMillis();
	            for(int m = 0;m < result.size();m++){
	                String element = result.get(m);
	                jedis.srem(key, element);
	            }
	            //long t2 = System.currentTimeMillis();
	            //
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(jedis!=null){
				jedis.close();
			}
		}
		return 1;
	}
	
	public long count(String key){
		Jedis jedis = null;
		 try {
			jedis = pool.getResource();
			 Long count = jedis.scard(key);
			 return count;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(jedis!=null){
				jedis.close();
			}
		}
		return -1;
	}
	
	public Boolean Sismember(String key,String member){
		Jedis jedis = null;
		Boolean sadd = false;
		 try {
			jedis = pool.getResource();
			sadd = jedis.sismember(key, member);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(jedis!=null){
				jedis.close();
			}
		}
		 return sadd;
	}
	
	public List<String> findAll(String key){
		Jedis jedis = null;
        List<String> list = new ArrayList<>();
		 try {
			jedis = pool.getResource();
			 //Long sadd = jedis.del(key);
			ScanParams scanParams = new ScanParams();
			scanParams.count(500);
			String cursor = "";
			while (!cursor.equals("0")) {
				ScanResult<String> scanResult = jedis.sscan(key, cursor, scanParams);
				cursor = scanResult.getStringCursor();
				List<String> result = scanResult.getResult();
				//long t1 = System.currentTimeMillis();
	            for(int m = 0;m < result.size();m++){
	                String element = result.get(m);
	                list.add(element);
	            }
	            //long t2 = System.currentTimeMillis();
	            //
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(jedis!=null){
				jedis.close();
			}
		}
		return list;
	}
}



